package com.zhangkeyou.map.mapweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhangkeyou.map.mapweb.domain.entity.Article;
import com.zhangkeyou.map.mapweb.domain.entity.Status;
import com.zhangkeyou.map.mapweb.service.ArticleService;
import com.zhangkeyou.map.mapweb.service.FileService;
import com.zhangkeyou.map.mapweb.service.UserinfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private FileService fileService;

//    @PreAuthorize("hasAnyAuthority('admin')")
    @GetMapping("/list")
    public Status getArticleList(){
        List<Article> articleList = articleService.getArticleList();
        return new Status<>(200,articleList,"success");
    }


    @GetMapping("/information")
    public Status getArticleInformation(@RequestParam(name = "id") String id){
        Article article = articleService.getArticleInformation(id);
        return new Status<>(200,article,"success");
    }

    @PostMapping("/ckeditor5")
    public Status updateArticleFromCkeditor5(@RequestBody Article article){
        if(article.getId().isEmpty()){
            JSONObject insert = articleService.insertArticleFromCkeditor5(article);
            return new Status(200,insert,"success");
        }
        else{
            JSONObject update = articleService.updateArticleFromCkeditor5(article);
            return new Status(200,update,"success");
        }
    }
    @PostMapping("/wangeditor")
    public Status updateArticleFromWangEditor(@RequestBody Article article){
        JSONObject update = articleService.updateArticleFromWangEditor(article);
        return new Status(200,update,"success");
    }

    @DeleteMapping("/ckeditor5")
    public Status deleteArticleByCkEditor5(@RequestBody Article article){
        JSONObject delete = articleService.deleteByCkEditor5(article);
        return new Status(200,delete,"success");
    }


    @DeleteMapping("/wangeditor")
    public Status deleteArticleByWangEditor(@RequestBody String articleId){
        JSONObject delete = articleService.deleteByWangEditor(articleId);
        return new Status(200,delete,"success");
    }
    @PostMapping("/fileid")
    public Status setArticleIdByFileId(@RequestBody Map<String,Object> map){
        String articleId = (String) map.get("articleId");
        List<String> fileIdList = (List<String>) map.get("fileIdList");
        JSONObject result = articleService.setArticleId(articleId,fileIdList);
        return new Status(result);
    }

    @GetMapping("/id")
    public Status getArticleId(){
        String id = articleService.getRandomArticleId().toString();
        return new Status(id);
    }
}
